package avicit.bdp.dcs.job.service;

import avicit.bdp.common.utils.SpringApplicationContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.util.Calendar;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;

@Component
public class JobTimer{

    private static final Logger logger = LoggerFactory.getLogger(JobTimer.class);

    static {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.HOUR_OF_DAY, 1);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);

        // 得出执行任务的时间,此处为今天的01：00：00
        Date time = calendar.getTime();

        Timer timer = new Timer();
        timer.schedule(new TimerTask() {
            public void run() {
                logger.info("JobTimer 开始执行删除作业");
                try{
                    DcsJobService jobService = SpringApplicationContext.getBean(DcsJobService.class);
                    jobService.deleteJob();
                    logger.info("JobTimer 成功执行删除作业");
                }catch(Exception e){
                    logger.error("JobTimer 删除作业失败,原因是:{}", e.getMessage());
                }
            }
        }, 1000 * 60, 1000 * 60 * 60);
        // 这里设定将延时每天固定执行


        // 定时计算job平均迁移速度
//        Timer jobTimer = new Timer();
//        jobTimer.schedule(new TimerTask() {
//            public void run() {
//                logger.info("JobTimer 开始执行计算作业速度");
//                try{
//                    LogService logService = SpringApplicationContext.getBean(LogService.class);
//                    logService.computeJobSpeed();
//                    logger.info("JobTimer 成功执行计算作业速度");
//                }catch(Exception e){
//                    logger.error("JobTimer 计算作业速度失败,原因是:{}", e.getMessage());
//                }
//            }
//        }, 1000 * 30, 1000 * 20);
    }

}
